#include "MR88FX02.h"
#include "Orion_Gpio.h"

void GPIO_Initial(GPIO_Type* GPIOX,enumPinNum PIN_Num,enumPinType conf,enumPinFunc funx)
{
	//-----------------------------------------FSR------------------------------
	//fsr select: input[0] output[1] af[2] analogy_function[3]

	uint8_t temp;
	temp=conf/8;
	SYSCTRL->AHBBCKCON		|=   0x08;	
	GPIOX->FSR &=	 (~((uint32_t)0x2<<(PIN_Num*2)));
	GPIOX->FSR |= 	(temp<<(PIN_Num*2));
	//-----------------------------------------FSL------------------------------
	if(PIN_Num<8)
	{
		GPIOX->DFSL &=	 (~((uint32_t)0xf<<(PIN_Num*4)));		//set DFSL =0
		GPIOX->DFSL |= 	(funx<<(PIN_Num*4));								//set DSFL =funx
	
	}
	else
	{
		GPIOX->DFSH &=	(~((uint32_t)0xf<<((PIN_Num-8)*4)));		//set DFSL =0
		GPIOX->DFSH |=	(funx<<((PIN_Num-8)*4));								//set DSFL =funx
	}

	//-----------------------------------------INEN------------------------------
	if(conf>=4 &&conf<=7)			
	{
		GPIOX->INEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
		GPIOX->INEN |= 	 ((uint16_t)0x1<<(PIN_Num));		//set 1
	}
	else if(conf<4)
	{
		GPIOX->INEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
	}
	else
		;//reserve
	//-------------------------------------ODEN---------------------------------- //OPEN DRAIN ENABLE
	if((conf>=12&&conf<=15) ||(conf>=20&&conf<=23))
	{
			GPIOX->ODEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
			GPIOX->ODEN |= 	 ((uint16_t)0x1<<(PIN_Num));		//set 1
	}
	else
		GPIOX->ODEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
	
	//----------------------------------PUEN-------------------------------------
	if(conf==2 ||conf==3||conf==6||conf==7||conf==10||conf==11||conf==14||conf==15||conf==18||conf==19||conf==22||conf==23)
	{
		GPIOX->PUEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
		GPIOX->PUEN |=		((uint16_t)0x1<<(PIN_Num));		//set 1
	}
	else
		GPIOX->PUEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
	
	//----------------------------------PDEN-------------------------------------
	if(conf%2==1)
	{
		GPIOX->PDEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
		GPIOX->PDEN |=		((uint16_t)0x1<<(PIN_Num));		//set 1
	}
	else
		GPIOX->PDEN &= (~((uint16_t)0x1<<(PIN_Num)));		//clear 0
	

}

void GPIO_Reset(GPIO_Type* GPIOX,enumPinNum PIN_Num)
{

	GPIOX->DO &= (~((uint32_t)0x1<<(PIN_Num)));		//clear 0
}	
void GPIO_Set(GPIO_Type* GPIOX,enumPinNum PIN_Num)
{
	GPIOX->DO &= (~((uint32_t)0x1<<(PIN_Num)));		//clear 0
	GPIOX->DO  |=	((uint16_t)0x1<<(PIN_Num));		//set 1
}	


BitStatue GPIO_Get_DO_statue(GPIO_Type* GPIOX,enumPinNum PIN_Num)
{
	if(GPIOX->DO &(0x1<<(PIN_Num)))
		return Set;
	else
		return Reset;
}
BitStatue GPIO_Get_DI_statue(GPIO_Type* GPIOX,enumPinNum PIN_Num)
{
	if(GPIOX->IN &(0x1<<(PIN_Num)))
		return Set;
	else
		return Reset;
}

void GPIO_Fout(enumFout fsrc)
{

	GPIO_Initial(GPIOB,Pin_Num_11,GPIO_AF_PP,Func2);
	SYSCTRL->PERCKEN_b.EXTICKEN=1;
	EXTI->FOUTSEL_b.FOUTSEL=fsrc;
}
